﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace Sac.Data
{
    /// <summary>
    /// 数据库操作类
    /// 作者：YZG
    /// 时间：2011-12-2
    /// </summary>
    public class OledbPro
    {
        private string strConn;
        public OleDbConnection Conn;
        private string errInfo;

        public string ErrInfo
        {
            get { return errInfo; }
        }

        /// <summary>
        /// 构造函数初始化连接对象
        /// </summary>
        /// <param name="OledbConnStr">连接字符串</param>
        public OledbPro(string OledbConnStr)
        {
            strConn = OledbConnStr;
            Conn = new OleDbConnection(strConn);
        }

        /// <summary>
        /// 打开数据库连接对象
        /// </summary>
        /// <returns></returns>
        public bool OledbConnOpen()
        {
            errInfo = "";
            bool Sign = true;
            try
            {
                if (Conn.State == ConnectionState.Closed)
                {
                    Conn.Open();
                }
            }
            catch (Exception e)
            {
                errInfo = "OledbConnOpen() Error! ErrorInformation:" + e.Message.ToString();
                Sign = false;
            }
            finally
            {
                
            }
            return Sign;
        }

        /// <summary>
        /// 关闭数据库连接对象
        /// </summary>
        /// <returns></returns>
        public bool OledbConnClose()
        {
            errInfo = "";
            bool Sign = true;
            try
            {
                if (Conn.State == ConnectionState.Open)
                {
                    Conn.Close();
                }
            }
            catch (Exception e)
            {
                errInfo = "OledbConnClose() Error! ErrorInformation:" + e.Message.ToString();
                Sign = false;
            }
            finally
            {
                
            }
            return Sign;
        }

        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql">执行的SQL语句</param>
        /// <returns>返回受影响的行数</returns>
        public int ExecuteNonQuery(string sql)
        {
            errInfo = "";
            int count = 0;
            OleDbCommand cmd = new OleDbCommand(sql, Conn);
            try
            {
                if (Conn.State == ConnectionState.Closed)
                {
                    Conn.Open();
                }
                count = cmd.ExecuteNonQuery();
            }
            catch(Exception e)
            {
                count = -999;
                errInfo = "ExecuteNonQuery() Error! ErrorInformation:" + e.Message.ToString();               
            }
            finally
            {
                cmd.Dispose();
                cmd = null;
                Conn.Close();
            }
            return count;
        }

        /// <summary>
        /// 运行查询语句并返回符合条件的结果的数目
        /// </summary>
        /// <param name="sql">查询SQL语句</param>
        /// <returns>返回符合查询条件结果</returns>
        public int ExecuteScalar(string sql)
        {
            errInfo = "";
            OleDbCommand cmd = new OleDbCommand(sql, Conn);
            int i = -1;
            try
            {
                if (Conn.State == ConnectionState.Closed)
                {
                    Conn.Open();
                }
                i = (int)cmd.ExecuteScalar();
            }
            catch(Exception e)
            {
                errInfo = "ExecuteScalar() Error! ErrorInformation:" + e.Message.ToString();
            }
            finally
            {
                cmd.Dispose();
                cmd = null;
                Conn.Close();
            }
            return i;
        }

        /// <summary>
        /// 根据SQL语句返回DataSet对象
        /// </summary>
        /// <param name="sql">SQL的Select语句</param>
        /// <returns></returns>
        public DataSet GetDataSet(string sql)
        {
            errInfo = "";
            OleDbDataAdapter da = new OleDbDataAdapter(sql, Conn);
            DataSet ds = new DataSet();
            try
            {
                if (Conn.State == ConnectionState.Closed)
                {
                    Conn.Open();
                }
                da.Fill(ds);
            }
            catch(Exception e)
            {
                errInfo = "GetDataSet() Error! ErrorInformation:" + e.Message.ToString();
            }
            finally
            {
                Conn.Close();
            }
            return ds;
        }

        /// <summary>
        /// 根据SQL语句返回DataTable对象
        /// </summary>
        /// <param name="sql">SQL语言的Select语句</param>
        /// <returns></returns>
        public DataTable GetDataTable(string sql)
        {
            errInfo = "";
            DataTable dt = null;
            try
            {
                dt = GetDataSet(sql).Tables[0];
            }
            catch(Exception e)
            {
                errInfo = "GetDataTable() Error! ErrorInformation:" + e.Message.ToString();
            }
            finally
            {
            }
            return dt;
        }

        /// <summary>
        /// 根据SQL语句返回符合条件的第一个DataRow
        /// </summary>
        /// <param name="sql">SQL语言的Select语句</param>
        /// <returns></returns>
        public DataRow GetDataRow(string sql)
        {
            errInfo = "";
            DataRow dr = null;
            try
            {
                dr = GetDataTable(sql).Rows[0];
            }
            catch (Exception e)
            {
                errInfo = "GetDataRow() Error! ErrorInformation:" + e.Message.ToString();
            }
            finally
            {
            }
            return dr;
        }

        /// <summary>
        /// 根据SQL语句返回符合条件OledbDataReader对象
        /// </summary>
        /// <param name="sql">SQL语言的Select语句</param>
        /// <returns></returns>
        public OleDbDataReader GetDataReader(string sql)
        {
            errInfo = "";
            OleDbDataReader dr = null;
            if (Conn.State == ConnectionState.Closed)
            {
                Conn.Open();
            }
            try
            {
                OleDbCommand cmd = new OleDbCommand(sql);
                dr = cmd.ExecuteReader();
            }
            catch (Exception e)
            {
                errInfo = "GetDataReader() Error! ErrorInformation:" + e.Message.ToString();
                dr.Dispose();
                dr = null;
            }
            finally
            {
            }
            return dr;
        }
    }
}
